<?php
/**
 * Created by PhpStorm.
 * User: ld
 * Date: 2018/4/30
 * Time: 上午10:11
 */
$params = array_merge(
    require(__DIR__ . '/../../common/config/params.php'),
    require(__DIR__ . '/params.php')
);
$modules = $modules = require(__DIR__ . '/modules.php');

return [
    'id' => 'app-api',
    'basePath' => dirname(__DIR__),
    'controllerNamespace' => 'api\controllers',
    'bootstrap' => ['log'],
    'modules' => $modules,
    'aliases' => [
        '@mdm/admin' => '@vendor/mdmsoft/yii2-admin',
    ],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-api',
        ],
        'user' => [
            'identityClass' => 'common\api\models\User',
            'enableAutoLogin' => true,
            'enableSession' => false,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest'],
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'enableStrictParsing' => true,
            'rules' => [
                [
                    'class' => 'yii\rest\UrlRule',
                    'controller' => ['user/user'],
                    'extraPatterns' => [
                        'POST login' => 'login',
                        'POST register' => 'register',
                        'POST update-channel-id' => 'update-channel-id',
                        'POST user-profile' => 'user-profile',
                        'POST upload-head-portrait' => 'upload-head-portrait',
                        'POST user-list' => 'user-list',
                        'POST change-password' => 'change-password',
                        'POST test' => 'test'
                    ]
                ]
            ],
        ],
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                $response->format = yii\web\Response::FORMAT_JSON;
                $code = $response->getStatusCode();
                $msg = !empty($response->data['msg']) ? $response->data['msg'] : yii\web\Response::$httpStatuses[$code];
                $data = !empty($response->data['data']) ? $response->data['data'] : [];
                $response->data = renderJSON($msg, $data, $code);
            },
        ],
    ],
    'language' => 'zh-CN',
    'params' => $params,
];